<!DOCTYPE HTML>
<html>
<head>
        <% include base.ejs %>
    <script src="js/underscore-noflect.js"></script>
    <!-- <script src="https://cdn.bootcss.com/underscore.js/1.8.3/underscore.js"></script> -->
    <script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=33823c697fdbbaafbf5a19a1c7c8f488"></script>
</head>
<body>
<% include head.ejs %>
<div class="col-sm-10 col-sm-offset-3 col-md-10 col-md-offset-2">
    <div class="news">
        <div class="news-sec">
            <div class="news-top" id="getAllArticle">

            </div>
            <div class="col-sm-12">
                <nav>
                    <ul class="pagination">
                    </ul>
                </nav>
            </div>
        </div>
        <div class="clearfix"> </div>
    </div>
</div>
<div class="clearfix"></div>
<!---->
<script type="text/template" id="allArticle">
    <div class="col-md-11 news-1">
        <a href="/article?ID={{= ID }}"><h5>{{= topic }}</h5></a>
        <h6>{{= date }}</h6>
        <div class="post-body">
            <p> {{= content.substring(0,200).replace(/<[^>]+>/g,"") }} </p>
        </div>
        <div class="news-footer">
            <h6 class="post-footer">
                分类：{{= classify }} <!--| 评论：未开放--> | 浏览：{{= visitNum }} | 点赞：{{= thumbsUp }} |
                <a href="/article?ID={{= ID }}">阅读全文 ></a>
            </h6>
        </div>
    </div>
</script>

<script type="text/javascript">
    var getAllArticle = $('#getAllArticle');
    //得到模板
    var compiled = _.template($("#allArticle").html());
    var type = getUrlParam("type");
    getPage(0);
    //AJAX读取文章列表
    function getPage(page) {
        $.post("/getArticle?type="+type+"&page="+page,function (result) {
            for(var i = 0; i<result.allResult.length; i++){
                var htmlstring = compiled(result.allResult[i]);
                getAllArticle.append(htmlstring);
            }
        });
    }
    //分页
    $.post("/getAllAmount?type="+type, function (result) {
        var amount = parseInt(result);
        pagement = Math.ceil(amount / 10);
        for(var i = 1; i <pagement+1;i++){
            $(".pagination").append("<li><a href='javascript:;'>" + i +"</a></li>");
        }
        $(".pagination li:first").addClass("active");
        $(".pagination li").click(function () {
            var page = $(this).index();
            $('#getAllArticle').html("");
            getPage(page);
            $(this).addClass("active").siblings().removeClass("active");
        });
    });

    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }

    var url = 'http://chaxun.1616.net/s.php?type=ip&output=json&callback=?&_='+Math.random();

    var map, geolocation;
    //加载地图，调用浏览器定位服务
    map = new AMap.Map('container', {
        resizeEnable: true
    });
    map.plugin('AMap.Geolocation', function() {
        geolocation = new AMap.Geolocation({
            enableHighAccuracy: true,//是否使用高精度定位，默认:true
            timeout: 1000,          //超过10秒后停止定位，默认：无穷大
            buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
            zoomToAccuracy: true,      //定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
            buttonPosition:'RB'
        });
        map.addControl(geolocation);
        geolocation.getCurrentPosition();
        AMap.event.addListener(geolocation, 'complete', onComplete);//返回定位信息
        AMap.event.addListener(geolocation, 'error', onError);      //返回定位出错信息
    });
    //解析定位结果
    function onComplete(data) {
        var lnglatXY = [data.position.getLng(),data.position.getLat()];
        regeocoder(lnglatXY);
    }
    //解析定位错误信息
    function onError(data) {
        $.getJSON(url, function(data){
            $.post("/getAddress",{
                "cxipAddress" : data.Ip,
                "cxIsp" : data.Isp,
                "cxBrowser" : data.Browser,
                "cxOS" : data.OS,
                "gdIsp" : "高德无法定位"
            },function (result) {

            });
        });
    }

    function regeocoder(lnglatXY) {  //逆地理编码
        var geocoder = new AMap.Geocoder({
            radius: 1000,
            extensions: "all"
        });
        geocoder.getAddress(lnglatXY, function(status, result) {
            if (status === 'complete' && result.info === 'OK') {
                geocoder_CallBack(result);
            }
        });
        var marker = new AMap.Marker({  //加点
            map: map,
            position: lnglatXY
        });
        map.setFitView();
    }
    function geocoder_CallBack(data) {
        var address = data.regeocode.formattedAddress; //返回地址描述
        $.getJSON(url, function(data){
            $.post("/getAddress",{
                "cxipAddress" : data.Ip,
                "cxIsp" : data.Isp,
                "cxBrowser" : data.Browser,
                "cxOS" : data.OS,
                "gdIsp" : address
            },function (result) {
                console.log(result);
            });
        });
    }
</script>

<!---->
</body>
</html>